﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using WeChat.JsSDK.lib;

namespace WeChat.Common
{
    public class KefuSendMsg
    {
        //给用户发送素材消息
        public static string sendMsg(string openid,string media_id)
        {
            string accesstoken = GetAccess_token.getAccesstoken();
            string url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accesstoken;

            string jsondata = "{\"touser\":\"" + openid + "\",\"msgtype\":\"mpnews\",\"mpnews\":{\"media_id\":\"" + media_id + "\"}}";
            //ApplicationLog.Write("发送客服消息json:" + jsondata);
            string jsons = new GetReturnJson().postReturnJson(url, jsondata);
            return jsons;
        }

        //给用户发送文本消息
        public static string sendText(string openid, string content)
        {
            string accesstoken = GetAccess_token.getAccesstoken();
            string url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accesstoken;

            string jsondata = "{\"touser\":\"" + openid + "\",\"msgtype\":\"text\",\"text\":{\"content\":\"" + content + "\"}}";
            //ApplicationLog.Write("发送客服消息json:" + jsondata);
            string jsons = new GetReturnJson().postReturnJson(url, jsondata);
            return jsons;
        }

        //获取客服状态
        public static string getKefuStates()
        {
            string accesstoken = GetAccess_token.getAccesstoken();
            string url = "https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist?access_token=" + accesstoken;

            string jsons = new GetReturnJson().getReturnJson(url);

            string jsons2 = HttpService.Get(url);
            //ApplicationLog.Write("获取客服状态json:" + jsons);
            //判断客服状态获取是否成功
            string kefuNames = "";
            if (!jsons.Contains("errcode"))
            {
                JObject jo = JObject.Parse(jsons);
                JArray ja = (JArray)JsonConvert.DeserializeObject(jo["kf_online_list"].ToString());
                
                foreach (JToken jt in ja)
                {
                    JObject je = (JObject)jt;
                    string m = je["kf_account"].ToString();
                    m = m.Substring(0, m.Length - 9);//截取字符串去掉账号后面的@sz_Jocat
                    if (jt == ja.Last)
                    {
                        kefuNames = kefuNames + m;
                    }
                    else
                    {
                        kefuNames = kefuNames + m + "\n";
                    }
                }
            }
            else//如果获取客服状态失败
            {
                //ApplicationLog.Write("获取客服状态失败：");
                kefuNames = "对不起，当前无客服在线/飞吻\n";
            }
            return kefuNames;
        }
    }
}
